﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using KeaoBase;
using System.IO;
using System.Reflection;
using System.Threading;

namespace KeaoMDS.Report
{
    public partial class ShiPinJianKong : BaseForm
    {
        private BaseVideo m_Video = null;
        private IntPtr m_Pic1 = IntPtr.Zero;
        private IntPtr m_Pic2 = IntPtr.Zero;
        private IntPtr m_Pic3 = IntPtr.Zero;
        private IntPtr m_Pic4 = IntPtr.Zero;
        private IntPtr m_Pic5 = IntPtr.Zero;
        private IntPtr m_Pic6 = IntPtr.Zero;
        public ShiPinJianKong()
        {
            InitializeComponent();
            m_Pic1 = pic_1.Handle;
            m_Pic2 = pic_2.Handle;
            m_Pic3 = pic_3.Handle;
            m_Pic4 = pic_4.Handle;
            m_Pic5 = pic_5.Handle;
            m_Pic6 = pic_6.Handle;
            //ThreadPool.QueueUserWorkItem(new WaitCallback(Init));
            Thread t = new Thread(new ParameterizedThreadStart(Init));
            t.Start();
        }

        private void Init(object Args)
        {
            if (KeaoUtils.ConfigGetNodeAttr("视频监控", "状态") == "启用")
            {
                m_Video = Assembly.LoadFrom("KeaoVideo.dll").CreateInstance(string.Format("KeaoVideo.{0}", KeaoUtils.ConfigGetNodeAttr("视频监控", "类型"))) as BaseVideo;
                if (m_Video.ConnectDevice(KeaoUtils.ConfigGetNodeAttr("视频监控", "地址"), KeaoUtils.ConfigGetNodeAttr("视频监控", "登录名"), KeaoUtils.Decrypt(KeaoUtils.ConfigGetNodeAttr("视频监控", "登录密码")), int.Parse(KeaoUtils.ConfigGetNodeAttr("视频监控", "端口号"))))
                {
                    if (KeaoUtils.ConfigGetNodeAttr("视频监控", "通道号1") != "禁用")
                    {
                        ChannelHandle channelHandle1 = new ChannelHandle() { Channel = int.Parse(KeaoUtils.ConfigGetNodeAttr("视频监控", "通道号1")), Handle = m_Pic1 };
                        m_Video.RealPlayByChannel(channelHandle1);
                    }
                    if (KeaoUtils.ConfigGetNodeAttr("视频监控", "通道号2") != "禁用")
                    {
                        ChannelHandle channelHandle2 = new ChannelHandle() { Channel = int.Parse(KeaoUtils.ConfigGetNodeAttr("视频监控", "通道号2")), Handle = m_Pic2 };
                        m_Video.RealPlayByChannel(channelHandle2);
                    }
                    if (KeaoUtils.ConfigGetNodeAttr("视频监控", "通道号3") != "禁用")
                    {
                        ChannelHandle channelHandle3 = new ChannelHandle() { Channel = int.Parse(KeaoUtils.ConfigGetNodeAttr("视频监控", "通道号3")), Handle = m_Pic3 };
                        m_Video.RealPlayByChannel(channelHandle3);
                    }
                    if (KeaoUtils.ConfigGetNodeAttr("视频监控", "通道号4") != "禁用")
                    {
                        ChannelHandle channelHandle4 = new ChannelHandle() { Channel = int.Parse(KeaoUtils.ConfigGetNodeAttr("视频监控", "通道号4")), Handle = m_Pic4 };
                        m_Video.RealPlayByChannel(channelHandle4);
                    }
                    if (KeaoUtils.ConfigGetNodeAttr("视频监控", "通道号5") != "禁用")
                    {
                        ChannelHandle channelHandle5 = new ChannelHandle() { Channel = int.Parse(KeaoUtils.ConfigGetNodeAttr("视频监控", "通道号5")), Handle = m_Pic5 };
                        m_Video.RealPlayByChannel(channelHandle5);
                    }
                    if (KeaoUtils.ConfigGetNodeAttr("视频监控", "通道号6") != "禁用")
                    {
                        ChannelHandle channelHandle6 = new ChannelHandle() { Channel = int.Parse(KeaoUtils.ConfigGetNodeAttr("视频监控", "通道号6")), Handle = m_Pic6 };
                        m_Video.RealPlayByChannel(channelHandle6);
                    }
                }
            }
        }

        private string m_TuPian1 = string.Empty;
        private string m_TuPian2 = string.Empty;
        private string m_TuPian3 = string.Empty;
        private string m_TuPian4 = string.Empty;
        private string m_TuPian5 = string.Empty;
        private string m_TuPian6 = string.Empty;

        public ShiPinJianKong(string TuPian1, string TuPian2, string TuPian3, string TuPian4, string TuPian5, string TuPian6)
        {
            InitializeComponent();
            m_TuPian1 = TuPian1;
            m_TuPian2 = TuPian2;
            m_TuPian3 = TuPian3;
            m_TuPian4 = TuPian4;
            m_TuPian5 = TuPian5;
            m_TuPian6 = TuPian6;
            
        }

        private void ShiPinJianKong_Load(object sender, EventArgs e)
        {
            
            if (m_Video == null)
            {
                if (File.Exists( m_TuPian1))
                {
                    pic_1.Image = Image.FromFile(m_TuPian1);
                }
                if (File.Exists(m_TuPian2))
                {
                    pic_2.Image = Image.FromFile(m_TuPian2);
                }
                if (File.Exists(m_TuPian3))
                {
                    pic_3.Image = Image.FromFile(m_TuPian3);
                }
                if (File.Exists(m_TuPian4))
                {
                    pic_4.Image = Image.FromFile(m_TuPian4);
                }
                if (File.Exists(m_TuPian5))
                {
                    pic_5.Image = Image.FromFile(m_TuPian5);
                }
                if (File.Exists(m_TuPian6))
                {
                    pic_6.Image = Image.FromFile(m_TuPian6);
                }
            }
        }

        private void pic_1_DoubleClick(object sender, EventArgs e)
        {
            PictureBox currentPic = sender as PictureBox;
            if (currentPic.Parent is TableLayoutPanel)
            {
                tableLayoutPanel1.Visible = false;
                currentPic.Parent = this;

            }
            else
            {
                tableLayoutPanel1.Visible = true;
                currentPic.Parent = tableLayoutPanel1;
                tableLayoutPanel1.Refresh();
            }
        }

        public event EventHandler OnClose;

        private void ShiPinJianKong_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (OnClose != null)
            {
                OnClose(null, null);
            }
            if (m_Video != null)
            {
                m_Video.CloseDevice();
            }
        }


    }
}
